package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.ChannelEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.UserLevelEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IChannelService;
import com.UnionPayCenter.service.IUserLevelService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: UnionPayCenter
 * @description:
 * @author: bite
 * @create:
 **/
@Slf4j
@Api(value = "userLevel", tags = "会员等级")
@RestController
@RequestMapping("/userLevel")
public class UserLevelController {
    @Autowired
    private IUserLevelService userLevelService;

    @RequestMapping(value = "/levels", method = RequestMethod.POST)
    @ApiOperation(tags = "会员等级", httpMethod = "POST", value = "[前端小程序]查询会员等级列表", response = UserLevelEntity.class)
    public ResponseWrapper findLevelList(
            @ApiJsonObject(name = "findLevelList", value = {
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int")
            }) @RequestBody JSONObject jsonParam) {
//        jsonParam.put("shopId",1);
        try {
            List<UserLevelEntity> list = userLevelService.findLevelList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端小程序]查询会员等级列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端小程序]查询会员等级列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "会员等级", httpMethod = "POST", value = "[后台]查询会员等级列表", response = UserLevelEntity.class)
    public ResponseWrapper findList(
            @ApiJsonObject(name = "userLevelFindListParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false)
            }) @RequestBody JSONObject jsonParam) {
        try {
            PageInfo<UserLevelEntity> list = userLevelService.findList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询会员等级列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询会员等级列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(tags = "会员等级", httpMethod = "POST", value = "[后台]会员等级添加", response = String.class)
    public ResponseWrapper add(
            @ApiJsonObject(name = "userLevelAddParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "name", value = "名称"),
                    @ApiJsonProperty(key = "condition", value = "等级条件", type = "int"),
                    @ApiJsonProperty(key = "discount", value = "等级折扣"),
                    @ApiJsonProperty(key = "sort", value = "排序", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            String name = jsonParam.getString("name");
            if(StringUtils.isNullStr(name)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "name"));
            }
            Integer condition = jsonParam.getInteger("condition");
            if(condition == null){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "condition"));
            }
            Integer sort = jsonParam.getInteger("sort");
            if(sort == null){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "sort"));
            }
            userLevelService.add(jsonParam);
            return ResponseWrapper.succeed("添加成功");
        }catch (ApiException e){
            log.error(String.format("[后台]添加会员等级失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]添加会员等级失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/update", method = RequestMethod.POST )
    @ApiOperation(tags = "会员等级", httpMethod = "POST", value = "[后台]会员等级修改", response = String.class)
    public ResponseWrapper update(
            @ApiJsonObject(name = "userLevelUpdateParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "levelId", value = "会员等级id", type = "int"),
                    @ApiJsonProperty(key = "name", value = "名称"),
                    @ApiJsonProperty(key = "condition", value = "等级条件", type = "int"),
                    @ApiJsonProperty(key = "discount", value = "等级折扣")
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer levelId = jsonParam.getInteger("levelId");
            if (levelId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "levelId"));
            }
            String name = jsonParam.getString("name");
            if(StringUtils.isNullStr(name)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "name"));
            }
            Integer condition = jsonParam.getInteger("condition");
            if(condition == null){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "condition"));
            }
            userLevelService.update(jsonParam);
            return ResponseWrapper.succeed("修改成功");
        }catch (ApiException e){
            log.error(String.format("[后台]修改会员等级失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]修改会员等级失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/updateTest", method = RequestMethod.POST )
    public ResponseWrapper updateTest( @RequestBody JSONObject jsonParam) {
            return ResponseWrapper.succeed("修改成功");
    }
}
